package com.winglish.as3.player 
{
	import com.winglish.as3.loader.SoundPlay;
	import com.winglish.as3.player.vo.ControlBar;
	
	import flash.events.Event;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	
	/**
	 * ...
	 * @author charmjin
	 */
	public class Mp3Player extends ControlBar 
	{
		private var sound:SoundPlay;
		private var autoPlay:Boolean;
		private var position:Number = 0;
		private var loadingBarMc:MovieClip;
		
		/**
		 * 자동 플레이, play버튼과 pause 버튼이 하나의 무비클립으로 사용이 되는지 설정.
		 * @param	autoPlay
		 * @param	sameBtn play 무비클립과 pause 무비클립이 하나의 무비클립으로 1,2번 프레임에 셋팅되어있다면 true.
		 */
		public function Mp3Player(autoPlay:Boolean = false, sameBtn:Boolean = false ) 
		{
			autoPlay = autoPlay;
			sameBtnUse = sameBtn;
		}
		
		/**
		 * sound 로드.
		 * @param	str
		 * @param   mc 자체 로딩바 mc
		 */
		public function init(str:String, mc:MovieClip):void {
			if(str){
				sound = new SoundPlay();
				if (mc) {
					loadingBarMc = mc;
					sound.addEventListener(ProgressEvent.PROGRESS, soundProgressEvent);
				}
				sound.addEventListener("loadComplete", soundLoadComplete);
				sound.soundLoader(str);
			}
			
		}
		
		/**
		 * 사운드 로딩 이벤트
		 * @param	e
		 */
		private function soundProgressEvent(e:ProgressEvent):void {
			loadingBarMc.scaleX = (e.bytesLoaded / e.bytesTotal);
		}
		
		/**
		 * sound 로드 완료 이벤트.
		 * @param	e
		 */
		
		private function soundLoadComplete(e:Event):void {
			if (autoPlay) sound.soundPlay();
			trace("sound load : " + e.target.getUrl);
			addEventListener(Event.ENTER_FRAME, enterEvent);
			dispatchEvent(new Event("soundLoad"));
		}
		
		/**
		 * 컨트롤바 볼 마우스 Up 이벤트.
		 * @param	e
		 */
		override protected function ballMouseUp(e:MouseEvent):void {
			super.ballMouseUp(e);
			if (sound) {
				if (sound.channel.position != sound.len) {
					sound.soundStop();
					sound.soundGotoPlay(position);
					dispatchEvent(new Event("playing"));
					if (sameBtnUse) {
					
					}else {
						if (playMc) playMc.gotoAndStop(2);
						if (pauseMc) pauseMc.gotoAndStop(1);
					}
				}else {
					sound.soundStop();
					if (sameBtnUse) {
					
					}else {
						if (playMc) playMc.gotoAndStop(1);
						if (pauseMc) pauseMc.gotoAndStop(2);
					}
				}
			}else {
				throw new Error( "로드 사운드가 없습니다." );
			}
		}
		
		/**
		 * 컨트롤바 볼 마우스 move 이벤트
		 * @param	e
		 */
		override protected function ballMouseMove(e:MouseEvent):void {
			super.ballMouseMove(e);
			var no:int = Math.floor((ballMc.x / barMc.width) * sound.len);
			position = no;
			sound.soundGotoPlay(position);
			sound.soundPause();
			e.updateAfterEvent();
		}
		
		/**
		 * play 클릭 이벤트.
		 * @param	e
		 */
		override protected function playMcClick(e:MouseEvent):void {
			if(e.currentTarget.currentFrame != 2){
				sound.soundPlay();
				super.playMcClick(e);
				dispatchEvent(new Event("playing"));
			}
		}
		
		/**
		 * pause 클릭 이벤트.
		 * @param	e
		 */
		override protected function pauseMcClick(e:MouseEvent):void {
			sound.soundPause();
			super.pauseMcClick(e);
		}
		
		/**
		 * replay 클릭 이벤트.
		 * @param	e
		 */
		override protected function replayBtnClick(e:MouseEvent):void {
			sound.soundGotoPlay(0);
			super.replayBtnClick(e);
			dispatchEvent(new Event("playing"));
		}
		
		/**
		 * stop 클릭 이벤트.
		 * @param	e
		 */
		override protected function stopMcClick(e:MouseEvent):void {
			sound.soundStop();
			super.stopMcClick(e);
		}

		
		/**
		 * 컨트롤바 이동 enterEvent 이벤트.
		 * @param	e
		 */
		protected function enterEvent(e:Event):void {
			if (!isDrag) {
				if (ballMc) {
					ballMc.x = int((sound.channel.position / sound.len) * barMc.width);
					stateMc.width = ballMc.x;
					//e.updateAfterEvent();
				}
				if (sound.channel.position == sound.len) {
					sound.soundStop();
					if (sameBtnUse) {
					
					}else {
						if (playMc) playMc.gotoAndStop(1);
						if (pauseMc) pauseMc.gotoAndStop(2);
						isDrag = false;
					}
				}
			}
		}
	}

}